#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author:Vergilgit pull origin master –allow-unrelated-histories
# datetime:2018/11/1 9:47
import json
import glob
import os
import sys
import time
import win32api, win32con
from sys import exit
from get_host_ip import get_host_ip
import BaseHTTPServer
from singleinstance import Singleinstance
from SimpleHTTPServer import SimpleHTTPRequestHandler
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from monitor_client.server_client import (
    get_base_infos,
    get_cpu_infos,
    get_memory_infos,
    get_disk_infos,
    get_network_infos,
    get_process_infos)
if not glob.glob(r'C:\Windows\Microsoft.NET\Framework\v4*\MSBuild.exe'):
    win32api.MessageBox(0, u'检测到本机未安装.Ne Framework 4.0,请先安装!', u'警告',win32con.MB_ICONWARNING)
    exit(0)
from monitor_client.server_temperature import get_temperature
from monitor_client.server_log import get_server_log

# host_ip = get_host_ip()
HandlerClass = SimpleHTTPRequestHandler
ServerClass = BaseHTTPServer.HTTPServer
Protocol = "HTTP/1.0"
# set_path()

class CustomHTTPRequestHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        self.send_head()

    def send_head(self):
        self.send_response(200)
        self.send_header('Content-type', 'application/json')
        self.end_headers()

        # print self.path
        if self.path == '/hostname/':
            try:
                d = os.popen('hostname').read().replace('\n','')
                # json.dumps(d)
                self.wfile.write(json.dumps(
                    {"state": "ok", 'data': d, 'message': "成功"}))
            except Exception as ex:
                pass

        elif self.path == '/base/':
            try:
                d = get_base_infos()
                # json.dumps(d)
                self.wfile.write(json.dumps(
                    {"state": "ok", 'data': d, 'message': "成功"}))
            except Exception as ex:
                pass
                # self.wfile.write(json.dumps(
                #     {"state": "error", 'data': [], 'message': ex.message}))

        elif self.path == '/temperature/':
            try:
                d = get_temperature()
                if d:
                    self.wfile.write(json.dumps(
                        {"state": "ok", 'data': d, 'message': "成功"}))
                else:
                    self.wfile.write(json.dumps(
                        {"state": "ok", 'data': u'无法获取温度', 'message': "设备暂不支持"}))
            except Exception as ex:
                # pass
                self.wfile.write(json.dumps(
                    {"state": "error", 'data': 'my mistake', 'message': ex.message}))

        elif self.path == '/cpu/':
            try:
                d = get_cpu_infos()
                # json.dumps(d)
                self.wfile.write(json.dumps(
                    {"state": "ok", 'data': d, 'message': "成功"}))
            except Exception as ex:
                pass
                # self.wfile.write(json.dumps(
                #     {"state": "error", 'data': [], 'message': ex.message}))
        elif self.path == '/memory/':
            try:
                d = get_memory_infos()
                self.wfile.write(json.dumps(
                    {"state": "ok", 'data': d, 'message': "成功"}))
            except Exception as ex:
                pass
                # self.wfile.write(json.dumps(
                #     {"state": "error", 'data': [], 'message': ex.message}))
        elif self.path == '/disk/':
            try:
                d = get_disk_infos()
                self.wfile.write(json.dumps(
                    {"state": "ok", 'data': d, 'message': "成功"}))
            except Exception as ex:
                # pass
                self.wfile.write(json.dumps(
                    {"state": "error", 'data': 'my mistake', 'message': ex.message}))
        elif self.path == '/network/':
            try:
                d = get_network_infos()
                self.wfile.write(json.dumps(
                    {"state": "ok", 'data': d, 'message': "成功"}))
            except Exception as ex:
                pass
                # self.wfile.write(json.dumps(
                #     {"state": "error", 'data': [], 'message': ex.message}))
        elif self.path == '/process/':
            try:
                d = get_process_infos()
                self.wfile.write(json.dumps(
                    {"state": "ok", 'data': d, 'message': "成功"}))
            except Exception as ex:
                pass
                # self.wfile.write(json.dumps(
                #     {"state": "error", 'data': [], 'message': ex.message}))
        elif self.path == '/log/':
            try:
                d = get_server_log()
                self.wfile.write(json.dumps(
                    {"state": "ok", 'data': d, 'message': "成功"}))
            except Exception as ex:
                pass
                # self.wfile.write(json.dumps(
                #     {"state": "error", 'data': [], 'message': ex.message}))
        else:
            self.wfile.write("hello world python \r\n")

    # path = self.translate_path(self.path)
    # print path

if __name__ == '__main__':
    myapp = Singleinstance()
    try:
        host_ip = sys.argv[1]
    except Exception:
        host_ip = get_host_ip()
    port = 20010
    server_address = (host_ip, port)
    try:
        if myapp.aleradyrunning():
            time.sleep(2)
            exit(0)
        HandlerClass.protocol_version = Protocol
        httpd = ServerClass(server_address, CustomHTTPRequestHandler)

        sa = httpd.socket.getsockname()
        print "Serving HTTP on", sa[0], "port", sa[1], "..."
        httpd.serve_forever()
    except Exception as e:
        print e
        input('123123')
